This is a Gopher daemon. Drag this application to any folder with text files, then launch this application. Those files (and any in any subfolders) will be available via Gopher on port 70.
*)
global socketsList
global newsockets
global thePath
global LF
global myIPAddress
on run
set LF to ASCII character (10)
set myIPAddress to (tcp my address)
set socketsList to {}
repeat with x from 1 to 2
set newSocket to {tcp wait for connect port 70}
set socketsList to socketsList & newSocket
end repeat
activate
set us to path to frontmost application as string
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set d to text items of us
set thePath to ((items 1 through -2 of d) as string) & ":"
set AppleScript's text item delimiters to oldDelimiters
end run
on handleFolder(s, l, newPath)
set allList to list folder newPath
set folderList to {}
set textList to {}
repeat with x in allList
set i to info for file (newPath & x)
if (folder of i = true) then
set folderList to folderList & x
else if (file type of i = "TEXT") then
set textList to textList & x
end if
end repeat
repeat with x in folderList
set sendLine to "1" & x & " " & l & x & ":" & " " & myIPAddress & " 70" & return
tcp write stream s data sendLine using ISO88591
end repeat
repeat with x in textList
set sendLine to "0" & x & " " & l & x & " " & myIPAddress & " 70" & return
tcp write stream s data sendLine using ISO88591
end repeat
end handleFolder
on handleFile(s, l, newPath)
tcp send stream s source file (newPath) using ISO88591
end handleFile
on handleConnection(s)
try
repeat until tcp ahead stream s characters LF
end repeat
set l to (tcp read stream s until characters LF strip characters return & LF using ISO88591)
if (offset of "::" in l) > 0 then
set l to ""
end if
set newPath to thePath & l
try
set i to info for file (newPath)
if (folder of i = true) then
handleFolder(s, l, newPath)
else
handleFile(s, l, newPath)
end if
on error
end try
tcp close stream s
on error msg number num from obj partial result pr
tcp close stream s
display dialog "Error: " & msg & " (" & num & ")"
error {msg, num, obj, pr}
end try
end handleConnection
on idle
set newsockets to {}
repeat with x from 1 to (length of socketsList)
set s to item x of socketsList
try
set itsStatus to (tcp status stream s)
if connection status of itsStatus = Connected then
-- if the socket is connected, start a new one waiting and handle the connection
set newsockets to newsockets & {tcp wait for connect port 70}